
package ucm.module;

import air_cp5620.UserInfo;
import air_cp5620.dao.DAO;
import javax.swing.JOptionPane;
import java.sql.*;
import ucm.view.UC_SelUserModResV;
import ucm.view.UC_SelUserResV;


public class UC_SelUserM {

    private DAO dbCon = null;

    public UC_SelUserM() {
        dbCon = new DAO();
    }



    public void selectByUserId_modify(String userid) {
        String sql = null;

        Connection con = dbCon.getConnection();
        if (con == null) {
            JOptionPane.showMessageDialog(null, "Don't conn！");
        }
        try {
            Statement stmt = con.createStatement(ResultSet.CONCUR_UPDATABLE, ResultSet.CONCUR_UPDATABLE);
            sql = "select * from tbl_user where userid='" + userid + "'" ;
            ResultSet res = stmt.executeQuery(sql);

            if (res.next()) {
                String roleid = res.getString("roleid");
                if (roleid.equals("100001")) {
                    roleid = "Ordinary Account";
                }
                else if (roleid.equals("100002")) {
                         roleid = "Account Manager";
                }
                else {
                         roleid = "Administrator";
                }

                String isdelete = res.getString("isdelete");
                if (isdelete.equals("1")) {
                    isdelete = "delete";
                }
                else {
                    isdelete = "online";
                }

                UserInfo userinfo = new UserInfo(res.getString("userid"),
                        res.getString("password"),
                        roleid,
                        isdelete);

                UC_SelUserResV uc_surv = new UC_SelUserResV(userinfo);
                uc_surv.setVisible(true);
            }
            else {
                JOptionPane.showMessageDialog(null, "User not found!");
            }

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Select user info false！");
            e.printStackTrace();
        }
    }


        public void selectByUserId_delete(String userid) {
        String sql = null;

        Connection con = dbCon.getConnection();
        if (con == null) {
            JOptionPane.showMessageDialog(null, "Don't conn！");
        }
        try {
            Statement stmt = con.createStatement(ResultSet.CONCUR_UPDATABLE, ResultSet.CONCUR_UPDATABLE);
            sql = "select * from tbl_user where userid='" + userid + "'" ;
            ResultSet res = stmt.executeQuery(sql);

            if (res.next()) {
                String roleid = res.getString("roleid");
                if (roleid.equals("100001")) {
                    roleid = "Ordinary Account";
                }
                else if (roleid.equals("100002")) {
                    roleid = "Account Manager";
                }
                else {
                    roleid = "Administrator";
                }

                String isdelete = res.getString("isdelete");
                if (isdelete.equals("1")) {
                    isdelete = "delete";
                }
                else {
                    isdelete = "online";
                }

                UserInfo userinfo = new UserInfo(res.getString("userid"),
                        res.getString("password"),
                        roleid,
                        isdelete);

                UC_SelUserModResV uc_sumrv = new UC_SelUserModResV(userinfo);
                uc_sumrv.setVisible(true);

            }
            else {
                JOptionPane.showMessageDialog(null, "User not found!");
            }

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Select user info false！");
            e.printStackTrace();
        }
    }
}
